package com.example.liguoying.rvpuanddown;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.liguoying.rvpuanddown.adapter.CanHolderHelper;
import com.example.liguoying.rvpuanddown.adapter.CanOnItemListener;
import com.example.liguoying.rvpuanddown.adapter.CanRVAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements CanRefreshLayout.OnRefreshListener, CanRecyclerViewHeaderFooter.OnLoadMoreListener {

    @BindView(R.id.can_content_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    CanRefreshLayout refresh;
    @BindView(R.id.footer)
    CanRecyclerViewHeaderFooter footer;

    @BindView(R.id.pb)
    ProgressBar pb;
    @BindView(R.id.tv_loadmore)
    TextView tvLoadmore;

    CanRVAdapter adapter;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        refresh.setOnRefreshListener(this);


        refresh.setStyle(0, 0);

        refresh.setMaxHeaderHeight((int) getResources().getDimension(R.dimen.dimen_super_super));

        refresh.setOnStartUpListener(new CanRefreshLayout.OnStartUpListener() {
            @Override
            public void onUp() {

                Log.e("Canyinghao", "onUp");
            }

            @Override
            public void onReset() {
                Log.e("Canyinghao", "onReset");
            }
        });

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL));


        adapter = new CanRVAdapter<MainBean>(recyclerView, R.layout.item_main) {


            @Override
            protected void setView(CanHolderHelper helper, int position, MainBean model) {
                if (position == 0) {
                    helper.setVisibility(R.id.tv_content, View.GONE);
                } else {

                    helper.setVisibility(R.id.tv_content, View.VISIBLE);
                }

                helper.setText(R.id.tv_content, model.content);
                helper.setText(R.id.tv_title, model.title);


            }

            @Override
            protected void setItemListener(CanHolderHelper helper) {


            }
        };

        recyclerView.setAdapter(adapter);


        adapter.setOnItemListener(new CanOnItemListener() {

            public void onRVItemClick(ViewGroup parent, View itemView, int position) {
                MainBean bean = (MainBean) adapter.getItem(position);

                Log.e(TAG, bean.title);
//                App.getInstance().show(bean.title);

            }


        });


        adapter.setList(MainBean.getList());

//        header.attachTo(recyclerView, true);
        footer.attachTo(recyclerView, false);
        footer.setLoadMoreListener(this);


    }

    int i = 0;

    @Override
    public void onLoadMore() {
        refresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.addMoreList(MainBean.getList());
                i++;
                if (i == 2) {
                    tvLoadmore.setVisibility(View.VISIBLE);
                    pb.setVisibility(View.GONE);
                    footer.setLoadEnable(false);
                }
                footer.loadMoreComplete();
            }
        }, 1000);

    }

    @Override
    public void onRefresh() {
        refresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.setList(MainBean.getList());
                refresh.refreshComplete();

                tvLoadmore.setVisibility(View.GONE);
                pb.setVisibility(View.VISIBLE);
                footer.setLoadEnable(true);

            }
        }, 1000);

    }
}
